package com.jdh.controller;

import com.jdh.model.ChatMessage;
import com.jdh.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @ClassName: ChatController
 * @Author: jdh
 * @CreateTime: 2022-11-17
 * @Description: websocket 接收和转发客户端消息的api接口类
 */
@RestController
@RequestMapping("/wsMqChat")
@EnableScheduling
public class ChatStompMqController {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    /**
     * 用于stompClient.send('/app/chat.sendMessage',header, JSON.stringify(body));测试接口
     * 以及消息转发的"/topic/public"的测试接口
     *
     * @param chatMessage
     * @param user
     * @return
     */
    @MessageMapping("/chatMq.sendMessage") // 定义消息发送的请求地址
    public ChatMessage sendMessage(@Payload ChatMessage chatMessage, User user, SimpMessageHeaderAccessor headerAccessor) {
        System.out.println("系统接收到的消息: " + chatMessage + " ; " + user);

        ChatMessage message = new ChatMessage();
        message.setSenderUuid(user.getUuid());
        message.setSenderName(chatMessage.getReceiverName());
        message.setSendContent("系统收到`" + user.getUuid() + "`的消息");
        message.setSendTime(new Date().toString());
        message.setReceiverUuid(user.getUuid());
        message.setReceiverName(chatMessage.getReceiverName());
        message.setMsgType("1");
        simpMessagingTemplate.convertAndSend("/topic/msg2." + user.getUuid(), message);
        System.out.println("系统回复消息:" + message);
        return message;
    }

    /**
     * 模拟接口推送
     * @param uuid
     * @return
     */
    @GetMapping("/wsPush")
    public String pushMsgToClient(@RequestParam String uuid){

        System.out.println("模拟推送消息，uuid:" + uuid);
        ChatMessage chatMessage = new ChatMessage();
        chatMessage.setSenderUuid(uuid);
        chatMessage.setReceiverUuid(uuid);
        chatMessage.setSendContent("这是模拟`" + uuid + "`推送的消息！");
        simpMessagingTemplate.convertAndSend("/websocket/pushMsg." + uuid, chatMessage);
        return uuid;
    }

    /**
     *
     * @param authorization
     * @param user
     */
    @SubscribeMapping("/ws://localhost:8083/chatMq")
    public void handleWebSocketConnect(@Header("Authorization") String authorization, @Header("user") String user) {
        // 访问和使用authorization和user参数
        System.out.println("Authorization: " + authorization);
        System.out.println("user: " + user);

        // 执行其他操作...
    }

}
